var functions = {}  
/**
 * H5判断当前是否是微信浏览器
 */
functions.isWechat=function(){  
	var ua = window.navigator.userAgent.toLowerCase();  
	if(ua.match(/micromessenger/i) == 'micromessenger'){  
		return true;  
	}else{  
		return false;  
	}  
}
/**
 * 当前时间戳
 */
functions.now=function(){
	return Date.parse(new Date())/1000;
}  
/** 通过秒级时间戳返回指定格式时间
（0返回完整时间,1返回日期，2返回时间，3返回每个节点数组,4返回时分）
 * @param {Object} time
 * @param {Object} type
 * @param {Object} wrong
 */
functions.datetime = function (time, type ) {
  type = type || 0; 
  var timestamp = parseFloat(time) > 0 ? time * 1000 : 0;
  var date = new Date(timestamp);

  var month = date.getMonth() + 1;
  var day = date.getDate(), hour = date.getHours(), min = date.getMinutes(), sec = date.getSeconds();
  var ret = {
    "y": date.getFullYear(),
    "m": month,
    "d": day,
    h: hour,
    "i": hour,
    's': sec,
    w: date.getDay(),
    "year": date.getFullYear()
  }
  if (month >= 1 && month <= 9) {
    month = "0" + month;
  }
  if (day >= 0 && day <= 9) {
    day = "0" + day;
  }
  if (hour >= 0 && hour <= 9) {
    hour = "0" + hour;
  }
  if (min >= 0 && min <= 9) {
    min = "0" + min;
  }
  if (sec >= 0 && sec <= 9) {
    sec = "0" + sec;
  }
  if (type == 3) {
    //简写ymdisw小于10不带0,星期不转化汉字
    ret.month = month;
    ret.day = day;
    ret.hour = hour;
    ret.second = sec;
    var weekday = ["日", "一", "二", "三", "四", "五", "六"];
    ret.weekday = weekday[date.getDay()];
    ret.date = date.getFullYear() + "-" + month + "-" + day;
    ret.time = hour + ":" + min + ":" + sec;
  } else if (type == 4) {
    ret = hour + ":" + min;
  } else if (type == 5) {
    ret = date.getFullYear() + "-" + month + "-" + day + " " + hour + ":" + min;
  } else if (type == 2) {
    ret = hour + ":" + min + ":" + sec;
  } else if (type == 1) {
    ret = date.getFullYear() + "-" + month + "-" + day;
  } else {
    ret = date.getFullYear() + "-" + month + "-" + day + " " + hour + ":" + min + ":" + sec
  }
  return ret;
}
/** 通过时间返回秒级时间戳
 * @param {Object} str 日期或完整时间 ，Y-m-d 或 Y-m-d H:i:s
 */
functions.strtotime = function (str) {
    var date = Date.parse(new Date(str.replace(/-/g, "/"))) / 1000;
    return date;
}
//分页参数-php
functions.initPage = function (page,objData) {  
	var initData= {
		 page:0,per_page:20,total:20,nomore:0
	}
	if(typeof(objData)!='undefined'){
		var obj={
			page:0,per_page:20,total:0,nomore:0
		};
		if(typeof(objData.current_page)!='undefined'){
			obj.page=objData.current_page;
			obj.total=objData.total;
			obj.per_page=objData.per_page;
			if(Math.ceil(objData.total/objData.per_page)<=parseInt(objData.page)){
				  obj.nomore=1; 
			}
		} 
		page._data = Object.assign(page._data,{paginate: obj})
	}else{ 
		page._data = Object.assign(page._data,{paginate:initData})
		if(typeof(page._data.paginate)!='undefined'){
			page._data.paginate=initData;
			return initData;
		}
		 
		
	}
	
   return page._data.paginate;
}
functions.nextPage = function (page) {
  var pag = page._data.paginate;
  let total_page=Math.ceil(pag.total/pag.per_page);
  
  if(total_page>parseInt(pag.page)){
	  pag.page++;
	  page._data.paginate=pag;
	  return pag;
  }else {
    return false;
  } 
} 
//分页参数-java
functions.initPageJ = function (page,objData) {  
	var initData= {
		 pages:1,pageSize:20,total:0,current:0,
	}
	if(typeof(objData)!='undefined'){
		var obj={
			current:0,pageSize:20,total:0,nomore:0,pages:1
		};
		if(typeof(objData.pageNum)!='undefined'){
			obj.current=objData.pageNum;
			obj.pages=objData.pages;
			obj.total=objData.total;
			obj.pageSize=objData.pageSize;
			 
		} 
		page._data = Object.assign(page._data,{paginate: obj})
	}else{ 
		page._data = Object.assign(page._data,{paginate:initData})
		if(typeof(page._data.paginate)!='undefined'){
			page._data.paginate=initData;
			return initData;
		}
		 
		
	}
	
   return page._data.paginate;
}
functions.nextPageJ = function (page) {
  var pag = page._data.paginate;  
  if(pag.pages>parseInt(pag.current)){
	  pag.current++;
	  page._data.paginate=pag;
	  return {
		  current:pag.current,
		  pageSize:pag.pageSize
	  };
  }else {
    return false;
  } 
} 
//加法函数，用来得到精确的加法结果  
functions.jia = function (arg1, arg2) {
  var r1, r2, m;
  try { r1 = arg1.toString().split(".")[1].length } catch (e) { r1 = 0 }
  try { r2 = arg2.toString().split(".")[1].length } catch (e) { r2 = 0 }
  m = Math.pow(10, Math.max(r1, r2));
  return (arg1 * m + arg2 * m) / m;

}
//乘法函数，用来得到精确的乘法结果    
functions.cheng = function (arg1, arg2) {
  var m = 0, s1 = arg1.toString(), s2 = arg2.toString();
  try { m += s1.split(".")[1].length } catch (e) { }
  try { m += s2.split(".")[1].length } catch (e) { }
  return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m);
}
//除法函数，用来得到精确的加法结果 
// a/b=chu(a,b)   
functions.chu = function (arg1, arg2) {
  var t1 = 0, t2 = 0, r1, r2;
  try { t1 = arg1.toString().split(".")[1].length } catch (e) { }
  try { t2 = arg2.toString().split(".")[1].length } catch (e) { }
  r1 = Number(arg1.toString().replace(".", ""))
  r2 = Number(arg2.toString().replace(".", ""))
  return (r1 / r2) * Math.pow(10, t2 - t1);

}
//减法函数   a-b=jian(a,b)
functions.jian = function (arg2, arg1) {
  var r1, r2, m, n;
  try { r1 = arg1.toString().split(".")[1].length } catch (e) { r1 = 0 }
  try { r2 = arg2.toString().split(".")[1].length } catch (e) { r2 = 0 }
  m = Math.pow(10, Math.max(r1, r2));
  //lastmodifybydeeka  
  //动态控制精度长度  
  n = (r1 >= r2) ? r1 : r2;
  return ((arg2 * m - arg1 * m) / m).toFixed(n);
}
/** 随机整数
 * @param {Object} min
 * @param {Object} max
 */
functions.rand = function (min, max) {
  return parseInt(Math.random() * (max - min + 1) + min, 10)
}
/** 随机字符串
 * @param {Object} len 长度
 * @param {Object} fuhao 是否包含特殊符号
 */
functions.randChar = function (len, fuhao) {
  var arr = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789,.?:[]!@#$%^&*()_+=-";
  var max = 61;
  if (typeof (fuhao) != "undefined") max = 82;
  len = (len == undefined || !parseInt(len)) ? 10 : len;
  var str = "";
  for (var i = 0; i < len; i++) {
    str += arr[this.rand(0, max)];
  }
  return str;
} 
//批量正则验证。并返回错误信息，基于uviewUI
functions.validate=function (_this,arr){ 
	var msg=true;
	for (var i in arr ){ 
		var label=arr[i][0];
		var value=arr[i][1];
		var type=arr[i][2];
		var extra=(typeof(arr[i][3])!=undefined)?arr[i][3]:''; 
		switch(type){ 
			case "require"://
				if(_this.$u.test.isEmpty(value)&&parseInt(value)!==0){
					msg=label+"不能为空";	
				}
				break; 
			case "username"://字母或数字,5到20位，
				if(!_this.$u.test.enOrNum(value)){
					msg=label+"为5-20位字母或数字"; 
				}else{
					if(value.length<5||value.length>20){
						 msg=label+"为5-20位字母或数字";
					}
				}
				break;
			case "password"://字母或数字，
				if(value.length<5||value.length>20){
					 msg=label+"为5-20位";
				}
				break;
			case "index"://indexOf验证
				if(extra.indexOf(value)<0){
					msg=label+"不正确";	
				}
				break;
			case "length"://长度验证 
				if(!_this.$u.test.rangeLength(value,extra)){
					if(extra[0]==extra[1]){
						msg=label+"长度"+extra[0]+"位";	
					}else{
						msg=label+"长度"+extra[0]+'到'+extra[1]+'位';
					}
				}
				break;
			case "range"://数字范围验证 
				if(!_this.$u.test.range(value,extra)){
					msg=label+"范围"+extra[0]+'到'+extra[1]; 
				}
				break;
			case "int"://整数
				if(!_this.$u.test.digits(value)){
					 msg=label+"必须是整数";
				}
				break;
			case "chinese"://chinese
				if(!_this.$u.test.chinese(value)){
					 msg=label+"必须是汉字";
				}
				break; 
			case "idcard"://身份证
				if(!_this.$u.test.idCard(value)){
					 msg=label+"格式不正确";
				}
				break;
			case "email"://邮箱
				if(!_this.$u.test.email(value)){
					 msg=label+"格式不正确";
				}
				break;
			case "amount"://金额
				if(!_this.$u.test.amount(value)){
					 msg=label+"格式不正确";
				}
				break;
			case "number"://数字
				if(!_this.$u.test.number(value)){
					 msg=label+"格式不正确";
				}
				break;
			case "char"://字母或数字
				if(!_this.$u.test.enOrNum(value)){
					msg=label+"为5-20位字母或数字"; 
				}else{
					if(value.length<5||value.length>20){
						 msg=label+"为5-20位字母或数字";
					}
				}
				break;
			default: 
				break;
		}
		console.log(msg);
		if(msg!==true){
			return msg;
		}
	}
	return true;
}


module.exports = functions